const db = require('../../db')
const { Builder, Browser, By, Key, until } = require('selenium-webdriver');
const clipboardy = require('clipboardy');
const axios = require('axios');
const cheerio = require('cheerio')
const fs = require('fs');

const example = async () => {
    let driver = await new Builder().forBrowser('chrome').build();
    let copyListText = []
    try {
        await driver.get('https://cs-going.com/zh/crosshair-library/');
        // 找到所有匹配的按钮
        let buttons = await driver.findElements(By.css('.p-4 .justify-between button'));
        // 循环点击
        for (let i = 0; i < buttons.length; i++) {
            buttons[i].click();
            copyListText[i] = await readClipboard()
        }
        await driver.quit();
        return copyListText
    } finally {
        // await driver.quit();
    }
}

async function readClipboard() {
    try {
        const text = await clipboardy.read();
        return text;
    } catch (err) {
        return null;
    }
}

function getFileNameFromUrl(url) {
    const parts = url.split('/');
    const fileNameWithExtension = parts[parts.length - 1];
    const fileName = fileNameWithExtension.split('.')[0];
    return fileName;
}

const getSightbead = async (req, res) => {
    try {
        return res.send({
            code: 200,
            data: JSON.parse(fs.readFileSync('./JSON/SightBead.json', 'utf-8')),
            msg: '数据获取成功'
        })
        const url = 'https://cs-going.com/zh/crosshair-library/';
        const { data } = await axios.get(url);
        const $ = cheerio.load(data);
        const imgItems = $('img')
        const tagItems = $('.bg-gray-600')
        const dataJson = []
        const tagList = []
        const configList = await example()

        tagItems.each((index, element) => {
            const tag = $(element).text()
            tagList.push(tag)
        })
        imgItems.each((index, element) => {
            const imgSrc = $(element).attr('src')
            const fileName = getFileNameFromUrl(imgSrc)
            dataJson.push({
                name: fileName,
                url: imgSrc,
                tags: index == 0 ? '' : tagList[index - 1],
                config: index == 0 ? '' : configList[index],
            })
        })
        if (!(dataJson.length > 0)) return res.send({
            code: 200,
            data: JSON.parse(fs.readFileSync('./JSON/SightBead.json', 'utf-8')),
            msg: '数据获取成功'
        })
        dataJson.shift()
        if (dataJson.length > JSON.parse(fs.readFileSync('./JSON/SightBead.json', 'utf-8')).length) {
            fs.writeFileSync('./JSON/SightBead.json', JSON.stringify(dataJson))
        }
        res.send({
            code: 200,
            msg: '数据获取成功',
            data: dataJson
        })
    } catch (err) {
        res.send({
            code: 500,
            msg: err
        })
    }
}

const getListAsync = (req, res) => {
    db.query('select * from t_everyone', (err, result) => {
        if (err) return res.send({ code: 500, message: err.message })
        res.send({
            code: 200,
            msg: '获取列表成功',
            data: result
        })
    })
}

module.exports = {
    getListAsync,
    getSightbead
}